package com.lzb.yabbs;

import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.lzb.yabbs.app.FindFragment;
import com.lzb.yabbs.app.ModuleFragment;
import com.lzb.yabbs.app.PersonalFragment;
import com.lzb.yabbs.interfaces.OnFragmentInteractionListener;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,OnFragmentInteractionListener {

    private String TAG=getClass().getSimpleName();

    private FrameLayout mFrameLayout;
    private LinearLayout mFindLl;
    private LinearLayout mMouduleLl;
    private LinearLayout mPersonalLl;

    private final int FIND_FRAGMENT=0;
    private final int MODULE_FRAGMENT=1;
    private final int PERSONAL_FRAGMENT=2;


    private Fragment mFindFragment;
    private Fragment mModuleFragment;
    private Fragment mPersonalFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mFindLl.setBackgroundColor(getResources().getColor(R.color.grey_300));
        setFragment(FIND_FRAGMENT);
    }


    private void initView(){
        mFrameLayout= (FrameLayout) findViewById(R.id.main_container);
        mFindLl= (LinearLayout) findViewById(R.id.find_ll);
        mMouduleLl= (LinearLayout) findViewById(R.id.module_ll);
        mPersonalLl= (LinearLayout) findViewById(R.id.personal_ll);

        mFindLl.setOnClickListener(this);
        mMouduleLl.setOnClickListener(this);
        mPersonalLl.setOnClickListener(this);
    }

    /**
     * 隐藏所有的Fragment
     * @param fragmentTransaction
     */
    private void hideAllFragment(FragmentTransaction fragmentTransaction){

        if(mFindFragment!=null){
            fragmentTransaction.hide(mFindFragment);
        }
        if(mModuleFragment!=null){
            fragmentTransaction.hide(mModuleFragment);
        }
        if(mPersonalFragment!=null){
            fragmentTransaction.hide(mPersonalFragment);
        }
    }
    /**
     * 选择fragment显示
     * @param fragmentId
     */
    private void setFragment(int fragmentId){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        hideAllFragment(fragmentTransaction);
        switch (fragmentId){
            case FIND_FRAGMENT:
                if(mFindFragment==null){
                    mFindFragment=new FindFragment();
                    fragmentTransaction.add(R.id.main_container,mFindFragment);
                }
                fragmentTransaction.show(mFindFragment);
                break;
            case MODULE_FRAGMENT:
                if(mModuleFragment==null){
                    mModuleFragment=new ModuleFragment();
                    fragmentTransaction.add(R.id.main_container,mModuleFragment);
                }
                fragmentTransaction.show(mModuleFragment);
                break;
            case PERSONAL_FRAGMENT:
                if(mPersonalFragment==null){
                    mPersonalFragment=new PersonalFragment();
                    fragmentTransaction.add(R.id.main_container,mPersonalFragment);
                }
                fragmentTransaction.show(mPersonalFragment);
                break;
        }
        fragmentTransaction.commit();
    }

    private void resetMenuColor(){
        mFindLl.setBackgroundColor(getResources().getColor(R.color.drawerArrowColor));
        mMouduleLl.setBackgroundColor(getResources().getColor(R.color.drawerArrowColor));
        mPersonalLl.setBackgroundColor(getResources().getColor(R.color.drawerArrowColor));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        resetMenuColor();
        switch (v.getId()){
            case R.id.find_ll:
                mFindLl.setBackgroundColor(getResources().getColor(R.color.grey_300));
                setFragment(FIND_FRAGMENT);
            break;
            case R.id.module_ll:
                mMouduleLl.setBackgroundColor(getResources().getColor(R.color.grey_300));
                setFragment(MODULE_FRAGMENT);
            break;
            case R.id.personal_ll:
                mPersonalLl.setBackgroundColor(getResources().getColor(R.color.grey_300));
                setFragment(PERSONAL_FRAGMENT);
            break;
            default:
                mFindLl.setBackgroundColor(getResources().getColor(R.color.grey_300));
                setFragment(FIND_FRAGMENT);
                break;
        }
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
